package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dao.entity.UserVocExampleSentenceDO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.DisplayUserVocSentenceDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserVocExampleSentenceCommand;
import com.cskaoyan.wordmemorize.service.UserPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/priviledge/voc")
public class UserPriviledgeVocController {
    @Autowired
    UserPrivilegeService userPrivilegeService;


    @PostMapping("example")
    public Result addExampleSentence(@RequestBody UserVocExampleSentenceCommand command) {
        Long loginId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.addUserVocSentence(command, loginId);
        return Result.ok();
    }

    @PutMapping("example")
    public Result updateExampleSentence(@RequestBody UserVocExampleSentenceCommand command) {
        long loginIdAsLong = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.updateUserVocSentence(command, loginIdAsLong);
        return Result.ok();
    }

    @GetMapping("/example/list")
    public Result<PageDTO<DisplayUserVocSentenceDTO>> getExampleSentenceList(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer count,
            @RequestParam(required = false) String keyword) {

        Long loginId = StpKit.USER.getLoginIdAsLong();

        // 创建并设置 PageRequest
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(pageNum);  // 使用 setter 方法设置页码
        pageRequest.setCount(count);      // 使用 setter 方法设置每页数量
        pageRequest.setKeyword(keyword);  // 使用 setter 方法设置关键词

        PageDTO<DisplayUserVocSentenceDTO> result = userPrivilegeService.getUserVocSentence(pageRequest, loginId);

        return Result.ok(result);
    }

}
